#!/usr/bin/python

#based on stuff from here:http://www.tutorialspoint.com/python/python_multithreading.htm

from threading import Thread
import time
from math import floor

exitFlag = 0

class myThread(Thread):
	threadID = None
	name = ''
	args = None
	def __init__(self, threadID, name, args):
		self.threadID = threadID
		self.name = name
		self.args = args
		Thread.__init__(self)
	def run(self):
		print "Running " + self.name
		cube()
		print "Exiting " + self.name

# Define a function for the thread
def print_time( args ):
	threadName, delay, counter = args
	count = 0
	while count < counter:
		if exitFlag:
			thread.exit()
		time.sleep(delay)
		count += 1
		print "%s: %s" % ( threadName, time.ctime(time.time()) )

def cube():
	for num in get_val():
		foo = str(num)+'	cubed	'+str(num*num*num)
		print foo

def get_val():
	for h in range(0,height):
		for w in range(0,width):
			yield my_arr[h][w]

num_threads = 2
threads = []

width = 5
height = 5

my_arr = [[0]*width]*height

for h in range(0,height):
	my_arr[h] = [0]*width
	for w in range(0,width):
		my_arr[h][w] = (h * width ) + w

part = int(floor(1.0*len(my_arr)/num_threads))
#cube, (my_arr[start:end]
for i in range(0,num_threads+1):
	start = i * part
	end = (i+1)*part
	# Create new threads
	threads.append(myThread(i, "Thread-"+str(i), None))

# Start new Threads
threads[1].start()
threads[2].run()

while threads[2].isAlive():
	if not thread[1].isAlive():
		exitFlag = 1
	pass
print "Exiting Main Thread"

